package civicinity.ist.psu.edu.adapter;

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import civicinity.ist.psu.edu.R;
import civicinity.ist.psu.edu.rssfeed.RSSItem;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CivicinityRssViewAdapter extends BaseAdapter{
	private Context mContext;
	private LayoutInflater mInflater;
	private ArrayList<RSSItem> mArr;
	private int mLayout;
	
	public CivicinityRssViewAdapter(Context context, int layout, ArrayList<RSSItem> arr) {
		mContext = context;
		mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mLayout = layout;
		mArr = arr;
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mArr.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mArr.get(position).getTitle();
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if(convertView == null)
			convertView = mInflater.inflate(mLayout, parent, false);
		
		ImageView vImage = (ImageView)convertView.findViewById(R.id.rss_image);
		TextView vTitle = (TextView)convertView.findViewById(R.id.rss_title);
		TextView vPubDate = (TextView)convertView.findViewById(R.id.rss_feed_date);

		vTitle.setText(mArr.get(position).getTitle());
		SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, yyyy HH:mm");
        Date resultdate = new Date(Long.parseLong(mArr.get(position).getPubDate()));
		vPubDate.setText(sdf.format(resultdate));
		// TODO Auto-generated method stub
		return convertView;
	}
}
